<?php

namespace backend\models;

use Yii;

/**
 * This is the model class for table "mall_product".
 *
 * @property int $id
 * @property int $aid 是否admin
 * @property int $mid 商家的user表的id
 * @property int $cid 分类id
 * @property string $title 商品名称
 * @property string $img 商品封面一张
 * @property string $imgs 商品展示图1-3张
 * @property string $detail 商品详情1-n张
 * @property string $detail_text 商品文字说明
 * @property string $old_price 原价
 * @property string $freight 运费
 * @property int $sort 排序
 * @property int $count_sale 售卖量
 * @property int $no 编号
 * @property string label 标签
 * @property string $level_one 等级1
 * @property string $level_two 等级2
 * @property int $is_up 默认1上架 -1 下架
 * @property int $up_time 最后一次上架时间
 * @property int $update_time 修改时间
 * @property int $create_time 创建时间
 * @property int $status 1正常 -1禁用
 * @property int $parent_id 父id,判断是否从上面复制下来
 * @property string $video_url 视频url
 */
class MallProduct extends \yii\db\ActiveRecord
{

    //定义场景
    const SCENARIO_UPDATE = 'update';
    const SCENARIO_UPLOAD = 'upload';
    const SCENARIO_CREATE = 'create';
    const SCENARIO_UPLOAD_POST = 'upload-post';
    const SCENARIO_UPLOAD_VIDEO = 'file-video';
    const SCENARIO_UPLOAD_DETAIL = 'upload-detail';

    public $file;

    public $files;

    public $file_video;

    public $file_detail;
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'mall_product';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['aid', 'mid', 'cid', 'sort', 'count_sale', 'is_up', 'up_time', 'update_time', 'create_time', 'status', 'parent_id'], 'integer'],
            [['mid', 'cid', 'title', 'img', 'imgs', 'old_price', 'up_time', 'update_time', 'create_time', 'freight'], 'required'],
            [['imgs','no','label', 'detail', 'detail_text', 'label', 'video_url'], 'string'],
            [['old_price','level_one', 'level_two', 'freight'], 'number'],
            [['title'], 'string', 'max' => 50],
            [['img'], 'string', 'max' => 255],
            [['file', 'files', 'file_detail'], 'file'],
            [['file', 'no', 'files', 'file_detail', 'video_url'], 'safe'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'aid' => '是否主商户',
            'mid' => '商家用户ID',
            'cid' => '分类ID',
            'title' => '产品名',
            'img' => '封面图',
            'imgs' => '展示图',
            'detail' => '详情图',
            'detail_text' => '详细说明',
            'old_price' => '原价',
            'freight' => '物流费',
            'sort' => '排序',
            'count_sale' => '销量',
            'no' => '产品编号',
            'is_up' => '是否上架',
            'label' => '属性名',
            'up_time' => '上架时间',
            'update_time' => '修改时间',
            'create_time' => '创建时间',
            'status' => '状态',
            'parent_id' => '父级ID',
            'video_url' => '视频',
        ];
    }

    /**
     * 跟用户表连接
     * @return \yii\db\ActiveQuery
     */
    public function getUser()
    {
        return $this->hasOne(EduUser::class, ['id'=> 'uid']);
    }

    /**
     * 跟产品分类连表
     * @return \yii\db\ActiveQuery
     */
    public function getCategory()
    {
        return $this->hasOne(MallCategory::class, ['id' => 'cid']);
    }

    /**
     * 关联属性表
     * @return \yii\db\ActiveQuery
     */
    public function getMallProductSpecs()
    {
        return $this->hasMany(MallProductSpecs::class, ['pid' => 'id']);
    }

    public static function attributeValue()
    {
        return [
            'status' => [
                '1' => '正常',
                '-1' => '禁用',
            ],
            'is_up' => [
                '1' => '上架',
                '-1' => '下架',
            ],
        ];
    }

    public function getStatusValue()
    {
        switch ($this->status)
        {
            case -1:
                $value = '禁用';
                break;
            case 1:
                $value = '正常';
                break;
            default:
                $value = '空';
                break;
        }
        return $value;
    }

    public function getIsUpValue()
    {
        switch ($this->is_up)
        {
            case -1:
                $value = '下架';
                break;
            case 1:
                $value = '上架';
                break;
            default:
                $value = '异常';
                break;
        }
        return $value;
    }

    public function scenarios()
    {
        $scenarios = parent::scenarios();
        $scenarios[self::SCENARIO_UPDATE] = ['aid', 'mid', 'cid', 'title', 'img', 'imgs', 'detail', 'detail_text', 'old_price', 'freight', 'sort', 'count_sale', 'is_up', 'up_time', 'up_time', 'update_time', 'create_time', 'status', 'parent_id', 'label', 'video_url'];
        $scenarios[self::SCENARIO_UPLOAD] = ['file'];
        $scenarios[self::SCENARIO_UPLOAD_POST] = ['files'];
        $scenarios[self::SCENARIO_UPLOAD_VIDEO] = ['file_video'];
        $scenarios[self::SCENARIO_CREATE] = ['aid', 'mid', 'cid', 'title', 'img', 'imgs', 'detail', 'detail_text', 'old_price', 'freight', 'sort', 'count_sale', 'is_up', 'up_time', 'up_time', 'update_time', 'create_time', 'status', 'parent_id', 'label', 'no', 'video_url'];
        $scenarios[self::SCENARIO_UPLOAD_DETAIL] = ['file_detail'];
        return $scenarios;
    }

    /**
     *作用：产生随机字符串，不长于32位
     * @param int $length
     * @return string
     */
    public function createNoncestr($length = 32)
    {
        $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
        $str = "";
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }

}
